﻿using System.IO;

namespace Comkarl.Web
{
    /// <summary>
    /// Represents a set of utilities that manages files' names.
    /// </summary>
    public static class FileNameUtility
    {
        /// <summary>
        /// Appends a version suffix before the file extension.
        /// </summary>
        /// <param name="fileName">The whole file name. It can be just a file name, relative or absolute path.</param>
        /// <param name="version">The version text (f.e. "1.0.0.0")</param>
        /// <returns></returns>
        public static string ChangeVersionInName(string fileName, string version)
        {
            if (!string.IsNullOrEmpty(version))
            {
                if (Path.HasExtension(fileName))
                {
                    return string.Format
                    (
                        "{0}{1}",
                        Path.ChangeExtension(fileName, version),
                        Path.GetExtension(fileName)
                    );
                }
                else
                {
                    fileName += '/';

                    return string.Format
                    (
                        "{0}.{1}",
                        Path.GetDirectoryName(fileName),
                        version
                    );
                }
            }
            else
            {

                return fileName;
            }
        }
    }
}